package com.example.mobile0123456789hwh0530;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainPSW;
    private CheckBox chkBoxMainName;
    private Button btnMainLogin;
    private Button btnMainReset;
//    声明SharedPreferences对象
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
//      在初始化方法中，获得SharedPreferences对象，如果不存在会自动创建
        sp = getSharedPreferences("login", MODE_PRIVATE);

//        点击chkBoxMainName保持选择状态
        chkBoxMainName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor=sp.edit();//获得编译器
                editor.putBoolean("chkName",b);//存入布尔类型数据
                editor.commit();//提交数据修改
            }
        });
//        点击chkBoxMainPSW保持选择状态
        chkBoxMainPSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor=sp.edit();//获得编译器
                editor.putBoolean("chkPSW",b);//存入布尔类型数据
                editor.commit();//提交数据修改
                if(b){
                    chkBoxMainName.setChecked(true);
                    chkBoxMainName.setEnabled(false);
                }else {
                    chkBoxMainName.setEnabled(true);
                }
            }
        });

        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                当账号密码都正确时，根据chkBoxMainName状态保存数据
                String userName=edtTxtMainUserName.getText().toString();
                String password=edtTxtMainPassword.getText().toString();
                if(userName.equals("admin")&&password.equals("admin")){
                    if(chkBoxMainName.isChecked()){
                        SharedPreferences.Editor editor=sp.edit();//获得编译器
                        editor.putString("userName",userName);//存入字符串类型数据
                        editor.commit();//提交数据修改
                    }
                    if(chkBoxMainPSW.isChecked()){
                        SharedPreferences.Editor editor=sp.edit();//获得编译器
                        editor.putString("password",password);//存入字符串类型数据
                        editor.commit();//提交数据修改

                    }
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        boolean chkName = sp.getBoolean("chkName", false);
        chkBoxMainName.setChecked(chkName);
        boolean chkPSW = sp.getBoolean("chkPSW", false);
        chkBoxMainPSW.setChecked(chkPSW);

        if(chkName){
            edtTxtMainUserName.setText(sp.getString("userName",null));
        }
        if(chkPSW){
            edtTxtMainPassword.setText(sp.getString("password",null));
        }
    }

    private void initView() {
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
        chkBoxMainName = (CheckBox) findViewById(R.id.chkBoxMainName);
        btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
        btnMainReset = (Button) findViewById(R.id.btnMainReset);
    }
}